from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.api.v1.api import api_router
from app.core.config import settings
from app.db.init_db import create_tables

app = FastAPI(
    title=settings.APP_NAME,
    description="为通用工具网站提供后端服务的 API。",
    version="0.1.0",
)

# Set all CORS enabled origins
if settings.DEBUG:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["http://localhost:5173"], # Vite frontend dev server
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

@app.on_event("startup")
async def on_startup():
    await create_tables()

app.include_router(api_router, prefix="/api/v1")

@app.get("/")
async def read_root():
    return {"message": f"Welcome to {settings.APP_NAME} API"}
